import router  from '@/router'
import { isRouteExist, isPathIncluded } from '@/utils/recursion-router'
import { useAuthStore } from '@/stores/auth'
import { useRoutesStore } from '@/stores/routes'
import {getToken} from "@/utils/auth"

router.beforeEach((to, from) => {
  const authStore = useAuthStore()
  const token = authStore.token
  console.log('token',token)
  const routesStore = useRoutesStore()
  const routes = routesStore.routes
  
  // if (!token) { // 未登录
  if (!getToken()) { // 未登录
     if (to.name !== 'login') {
       return { name: 'login' }
     } else {
       return true
     }
   } else {
     if (to.name === 'login') { // 已登录 但 去往登录页
       return { name: 'home' }
     } else if (!isPathIncluded(to.path, routes) && to.name !== '403' && to.name !== '404'){
       return { name: '403' }
     } else {
       return true
     }
   }
})
